package deleteface.bank.jqsoft.cn.deleteface.http.presenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;

import com.trello.rxlifecycle.ActivityEvent;

import javax.inject.Inject;
import javax.inject.Named;

import deleteface.bank.jqsoft.cn.deleteface.base.AbstractActivity;
import deleteface.bank.jqsoft.cn.deleteface.base.BphsConstants;
import deleteface.bank.jqsoft.cn.deleteface.base.SignedDoctorClientService;
import deleteface.bank.jqsoft.cn.deleteface.http.BasePresenter;
import deleteface.bank.jqsoft.cn.deleteface.http.contract.HttpResultBaseGWBean;
import deleteface.bank.jqsoft.cn.deleteface.http.contract.PeopleBaseFragmentContract;
import deleteface.bank.jqsoft.cn.deleteface.setip.Util;
import deleteface.bank.jqsoft.cn.deleteface.webservice.RequestManager;
import okhttp3.ResponseBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/5/21.
 */

public class PeopleBaseFragmentPresenter extends BasePresenter<PeopleBaseFragmentContract.View> implements PeopleBaseFragmentContract.presenter {

    private final PeopleBaseFragmentContract.View view;
    private final SharedPreferences sharedPreferences;
    private final SignedDoctorClientService signedDoctorClientService;


    @Inject
    public PeopleBaseFragmentPresenter(PeopleBaseFragmentContract.View view,
                                       @Named("default") SharedPreferences sharedPreferences,
                                       SignedDoctorClientService signedDoctorClientService) {
        this.view = view;
        this.sharedPreferences = sharedPreferences;
        this.signedDoctorClientService = signedDoctorClientService;
    }

    public void runNote(String body,String url) {
        final Context context = (Context) view;
        AbstractActivity abstractActivity = (AbstractActivity) context;

            RequestManager.getInstance(url)
                    .mServiceStore
                    .applyDelete(body)
                    .subscribeOn(Schedulers.io())
                    .compose(abstractActivity.<ResponseBody>bindUntilEvent(ActivityEvent.DESTROY))
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<ResponseBody>() {
                        @Override
                        public void onStart() {
                            super.onStart();
                            Util.showGifProgressDialog(context);

                        }

                        @Override
                        public void onCompleted() {
                            Util.hideGifProgressDialog(context);
                        }

                        @Override
                        public void onError(Throwable e) {
                            Util.hideGifProgressDialog(context);
                            view.onDeleteFailure(e.getMessage());

                        }

                        @Override
                        public void onNext(ResponseBody responseBody) {

                            String json = Util.DecodeXmlJosn(responseBody);
                            HttpResultBaseGWBean result = new Gson().fromJson(json, HttpResultBaseGWBean.class);
                            if (result != null) {
                                if (result.getsSuccess().equals("1")) {
                                    view.onDeletFaceSuccess(result);
                                } else if (result.getsSuccess().equals("0")) {
                                    String msg = result.getsMessage();
                                    view.onDeleteFailure(msg);
                                }
                            }
                        }

                    });




    }

    public void runNoteGuizhou(String body,String url) {
        final Context context = (Context) view;
        AbstractActivity abstractActivity = (AbstractActivity) context;

        RequestManager.getInstance(url)
                .mServiceStore
                .applyDeleteGuizhou(body)
                .subscribeOn(Schedulers.io())
                .compose(abstractActivity.<ResponseBody>bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Util.showGifProgressDialog(context);

                    }

                    @Override
                    public void onCompleted() {
                        Util.hideGifProgressDialog(context);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Util.hideGifProgressDialog(context);
                        view.onDeleteFailure(e.getMessage());

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {

                        String json = Util.DecodeXmlJosn(responseBody);
                        HttpResultBaseGWBean result = new Gson().fromJson(json, HttpResultBaseGWBean.class);
                        if (result != null) {
                            if (result.getsSuccess().equals("1")) {
                                view.onDeletFaceSuccess(result);
                            } else if (result.getsSuccess().equals("0")) {
                                String msg = result.getsMessage();
                                view.onDeleteFailure(msg);
                            }
                        }
                    }

                });




    }


    public void deleteWPReadCardInfo(String body,String url) {
        final Context context = (Context) view;
        AbstractActivity abstractActivity = (AbstractActivity) context;
        RequestManager.getInstance(url)
                .mServiceStore
                .applyWPReadCardInfo(body)
//                .compose(abstractActivity.<ResponseBody>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .compose(abstractActivity.<ResponseBody>bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Util.showGifProgressDialog(context);

                    }

                    @Override
                    public void onCompleted() {
                        Util.hideGifProgressDialog(context);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Util.hideGifProgressDialog(context);
                        view.onDeleteFailure(e.getMessage());

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {

                        String json = Util.DecodeXmlJosn(responseBody);
                        HttpResultBaseGWBean result = new Gson().fromJson(json, HttpResultBaseGWBean.class);
                        if (result != null) {
                            if (result.getsSuccess().equals("1")) {
                                view.onDeletFaceSuccess(result);
                            } else if (result.getsSuccess().equals("0")) {
                                String msg = result.getsMessage();
                                view.onDeleteFailure(msg);
                            }
                        }
                    }

                });

    }


    public void deleteWPReadCardInfoGuizhou(String body,String url) {
        final Context context = (Context) view;
        AbstractActivity abstractActivity = (AbstractActivity) context;
        RequestManager.getInstance(url)
                .mServiceStore
                .applyWPReadCardInfoGuizhou(body)
//                .compose(abstractActivity.<ResponseBody>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .compose(abstractActivity.<ResponseBody>bindUntilEvent(ActivityEvent.DESTROY))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        Util.showGifProgressDialog(context);

                    }

                    @Override
                    public void onCompleted() {
                        Util.hideGifProgressDialog(context);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Util.hideGifProgressDialog(context);
                        view.onDeleteFailure(e.getMessage());

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {

                        String json = Util.DecodeXmlJosn(responseBody);
                        HttpResultBaseGWBean result = new Gson().fromJson(json, HttpResultBaseGWBean.class);
                        if (result != null) {
                            if (result.getsSuccess().equals("1")) {
                                view.onDeletFaceSuccess(result);
                            } else if (result.getsSuccess().equals("0")) {
                                String msg = result.getsMessage();
                                view.onDeleteFailure(msg);
                            }
                        }
                    }

                });

    }
}